package com.lcicl.reggie.controller;
/*
  Date: 2023/08/10
  Author: Luochao
  Description:
        分类管理
*/
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcicl.reggie.common.R;
import com.lcicl.reggie.entity.Category;
import com.lcicl.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {

        log.info("page:{}\tpageSize:{}", page, pageSize);

        Page<Category> pageInfo = new Page<>(page, pageSize);

        //查询wrapper
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);

        Page<Category> rePage = categoryService.page(pageInfo, queryWrapper);

        return R.success(rePage);

    }

    /**
     * 根据条件分页查询
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List> lst(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, Category::getType,type);
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping()
    public R<String> add(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 删除分类
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> delete(Long ids) {

        boolean flag = categoryService.delete(ids);

        return flag ? R.success("删除成功") : R.error("删除失败");

    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @PutMapping()
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");

    }

}


